//========================================================================================
//  
//  $File: //depot/indesign_CS5.5/source/sdksamples/docwatch/DocWchResponder.cpp $
//  
//  Owner: Adobe Developer Technologies
//  
//  $Author: pmbuilder $
//  
//  $DateTime: 2010/02/23 03:46:08 $
//  
//  $Revision: #1 $
//  
//  $Change: 742150 $
//  
//  Copyright 1997-2010 Adobe Systems Incorporated. All rights reserved.
//  
//  NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance 
//  with the terms of the Adobe license agreement accompanying it.  If you have received
//  this file from a source other than Adobe, then your use, modification, or 
//  distribution of it requires the prior written permission of Adobe.
//  
//========================================================================================

#include "VCPlugInHeaders.h"

// Interface includes:
#include "IClassIDData.h"
#include "isignalmgr.h"
#include "IDocumentSignalData.h"
#include "IUIDData.h"
#include "ISubject.h"
#include "IObserver.h"

// Implementation includes:
#include "CreateObject.h"
#include "CResponder.h"
#include "GenericID.h"

#include "WilID.h"

#include "csxs_facade.h"
#include "EventosWilson.h"
#include <boost/shared_ptr.hpp>


using namespace eventos;
/**
 Lo uso para inicializar CSXS la primera vez que se crea o que se abre un documento. Antes estaba en un StartupShutdownService
 pero a esa altura daba un error porque las librerias de PluPlug no estaba inicializadas todavia y no te podias registrarte
 a escuchar ningun evento.
*/
class WilDocWchResponder : public CResponder
{
	public:
	
		/**
			Constructor.
			@param boss interface ptr from boss object on which this interface is aggregated.
		*/
		WilDocWchResponder(IPMUnknown* boss);

		/**
			Respond() handles the file action signals when they
			are dispatched by the signal manager.  This implementation
			simply creates alerts to display each signal.

			@param signalMgr Pointer back to the signal manager to get
			additional information about the signal.
		*/
		virtual void Respond(ISignalMgr* signalMgr);

private:
	
	boost::shared_ptr<listar_notas_request> eln;
	boost::shared_ptr<crear_nota_request> ecn;
	boost::shared_ptr<agregar_comentario_request> enc;
	boost::shared_ptr<listar_comentarios_request> lcr;
	boost::shared_ptr<seleccionar_marcador_request> snr;
	boost::shared_ptr<ocultar_notas_request> onr;
	boost::shared_ptr<eliminar_nota_request> enr;
	
	void registrar_eventos_csxs();
	void atachar_observer(IDocumentSignalData * signal_data);
	void detachar_observer(IDocumentSignalData * signal_data);
};


/* CREATE_PMINTERFACE
 Binds the C++ implementation class onto its ImplementationID 
 making the C++ code callable by the application.
*/
CREATE_PMINTERFACE(WilDocWchResponder, kWilDocWatchResponderImpl)

/* DocWchActionComponent Constructor
*/
WilDocWchResponder::WilDocWchResponder(IPMUnknown* boss) :
	CResponder(boss)
{
}

/* Respond
*/
void WilDocWchResponder::Respond(ISignalMgr* signalMgr)
{	
	// Get the service ID from the signal manager
	ServiceID serviceTrigger = signalMgr->GetServiceID();

	// Get a UIDRef for the document.  It will be an invalid UIDRef
	// for BeforeNewDoc, BeforeOpenDoc, AfterSaveACopy, and AfterCloseDoc because
	// the document doesn't exist at that point.
	InterfacePtr<IDocumentSignalData> docData(signalMgr, UseDefaultIID());
	if (docData == nil)
	{
		ASSERT_FAIL("Invalid IDocumentSignalData* - DocWchResponder::Respond");
		return;
	}
	UIDRef doc = docData->GetDocument();

	// Take action based on the service ID
	switch (serviceTrigger.Get())
	{
		case kBeforeNewDocSignalResponderService:
		{
			
			break;
		}
		case kDuringNewDocSignalResponderService:
		{
			
			break;
		}
		case kAfterNewDocSignalResponderService:
		{
			atachar_observer(docData);
			registrar_eventos_csxs();
			break;
		}
		case kBeforeOpenDocSignalResponderService:
		{
			
			break;
		}
		case kDuringOpenDocSignalResponderService:
		{
			
			break;
		}
		case kAfterOpenDocSignalResponderService:
		{
			registrar_eventos_csxs();
			atachar_observer(docData);
			break;
		}
		case kBeforeSaveDocSignalResponderService:
		{
			
			break;
		}
		case kAfterSaveDocSignalResponderService:
		{
			
			break;
		}
		case kBeforeSaveAsDocSignalResponderService:
		{
			
			break;
		}
		case kAfterSaveAsDocSignalResponderService:
		{
			
			break;
		}
		case kBeforeSaveACopyDocSignalResponderService:
		{
			
			break;
		}
		case kDuringSaveACopyDocSignalResponderService:
		{
			
			break;
		}
		case kAfterSaveACopyDocSignalResponderService:
		{
			
			break;
		}
		case kBeforeRevertDocSignalResponderService:
		{
			
			break;
		}
		case kAfterRevertDocSignalResponderService:
		{
			
			break;
		}
		case kBeforeCloseDocSignalResponderService:
		{
			detachar_observer(docData);
			break;
		}
		case kAfterCloseDocSignalResponderService:
		{
			
			break;
		}

		default:
			break;
	}
}

void WilDocWchResponder::atachar_observer(IDocumentSignalData * signal_data)
{
	UIDRef doc_ref = signal_data->GetDocument();
	InterfacePtr<IObserver> observer(doc_ref,IID_IWILNOTEOBSERVER);
	InterfacePtr<ISubject> subject(doc_ref,UseDefaultIID());
	if (observer && subject 
		&& !subject->IsAttached(observer,IID_IHIERARCHY_DOCUMENT,IID_IWILNOTEOBSERVER)) {
		subject->AttachObserver(observer,IID_IHIERARCHY_DOCUMENT,IID_IWILNOTEOBSERVER);
	}
}

void WilDocWchResponder::detachar_observer(IDocumentSignalData * signal_data)
{
	UIDRef doc_ref = signal_data->GetDocument();
	InterfacePtr<IObserver> observer(doc_ref,IID_IWILNOTEOBSERVER);
	InterfacePtr<ISubject> subject(doc_ref,UseDefaultIID());
	if (observer && subject 
		&& subject->IsAttached(observer,IID_IHIERARCHY_DOCUMENT,IID_IWILNOTEOBSERVER)) {
			
			subject->DetachObserver(observer,IID_IHIERARCHY_DOCUMENT,IID_IWILNOTEOBSERVER);
	}
}
			
void WilDocWchResponder::registrar_eventos_csxs()  
{
	static bool eventos_registrados = false;
	if (eventos_registrados) return; 
	eventos_registrados = true;
	eln.reset(new listar_notas_request());
    ecn.reset(new crear_nota_request());
	enc.reset(new agregar_comentario_request());
	lcr.reset(new listar_comentarios_request());
	snr.reset(new seleccionar_marcador_request());
	onr.reset(new ocultar_notas_request());
	enr.reset(new eliminar_nota_request());
	
	wilson::escuchar(eln->ID(),eln.get());
	wilson::escuchar(ecn->ID(),ecn.get());
	wilson::escuchar(enc->ID(),enc.get());
	wilson::escuchar(lcr->ID(),lcr.get());
	wilson::escuchar(snr->ID(),snr.get());
	wilson::escuchar(onr->ID(),onr.get());
	wilson::escuchar(enr->ID(),enr.get());
}

// End, WilDocWchResponder.cpp.



